#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
class dTree{
	private:
		int *father;
		int *value;
		int *leaf;
		int *del;
		int *len;
		int *pass;
		int r0;
		int n;
		int d;
	public:
		dTree(int a,int b, istream &in = cin) {
			n=a;
			d=b;
			r0=0;
			father=new int[a];
			value=new int[a];
			leaf=new int[a];
			del=new int[a];
			len=new int[a];
			pass=new int[a];
			int i,j,chx,k;
			for(i=0;i<a;i++){
				father[i]=-1;
				value[i]=0;
				leaf[i]=1;
				del[i]=0;
				len[i]=0;
				pass[i]=0;
			}
			for(i=0;i<a;i++){
                in>>chx;
				for(j=0;j<chx;j++){
					leaf[i]=0;
                    in>>k;
					father[k]=i;
                    in>>value[k];
				}
			}
		}
		void givelen(){
			int i,j,j0,x;
			for(i=0;i<n;i++){
				if(pass[i]==0){
					len[i]=0;
					leaf[i]=1;
				}
			}
			for(i=0;i<n;i++){
				if(del[i]==0&&pass[i]==0&&father[i]!=-1){
					j=father[i];
					leaf[j]=0;
				}
			}
			for(i=0;i<n;i++){
				if(leaf[i]==1&&father[i]!=-1&&pass[i]==0){
					len[i]=0;
					j=i;
					j0=father[j];
					x=0;
					while(j!=-1){
						x=x+value[j];
						if(len[j0]<x) len[j0]=x;
						j=father[j];
						j0=father[j];
					}
				}
			}
		}
		int nopass(){
			int i;
			for(i=0;i<n;i++){
				if(pass[i]==0) return 0;
			}
			return 1;
		}
		void shuchu(){
			for(int i=0;i<n;i++){
				cout<<i<<" father="<<father[i]<<" value="<<value[i]<<" leaf="<<leaf[i]<<" del="<<del[i]<<" len="<<len[i]<<" pass="<<pass[i]<<endl;
			}
		}
		void solution0(){
			int i,j,k,j0;
			givelen();
			//shuchu();
			i=n-1;
			while(i>=1){
				j=father[i];
				if(del[i]==0&&len[j]>d){
                    cout<<i<<"-----del "<<j<<",len:"<<len[j]<<endl;
					del[j]=1;
					r0++;
					
					k=i;
					while(father[k]==j) k--;
					i=k;
                    // cout<<"new i "<<i<<endl;
					
					
					//j=father[j];
					//if(j==-1) break;
					//j0=len[j];
					int k1;
					k1=j;
					j=father[j];
					j0=len[j];
					if(j==-1) break;
					k=k1;
					
					
					len[j]=0;
					while(father[k]==j){
						if(value[k]+len[k]!=j0&&del[k]==0){
                            cout<<"start"<<k<<","<<father[k]<<endl;
							if(value[k]+len[k]>len[j]){
								len[j]=value[k]+len[k];
							}
						}
						k--;
					}
                     cout<<"over "<<k<<","<<father[k]<<" "<<j<<",len:"<<len[j]<<endl;
					k=k1;

					while(father[k]==j){
						if(value[k]+len[k]!=j0&&del[k]==0){
                            	cout<<"start1 "<<k<<","<<father[k]<<endl;
							if(value[k]+len[k]>len[j]){
								len[j]=value[k]+len[k];
							}
						}
						k++;
					}
                    cout<<"over1 "<<k<<","<<father[k]<<" "<<j<<",len:"<<len[j]<<endl;
				}

				else{
					j=i;
                    // cout<<i<<"-----------no del ";
					i--;
                    //	cout<<"new i "<<i<<endl;

					int k1;
					k1=j;
					j=father[j];
					j0=len[j];
					if(j==-1) break;
					k=k1;

					len[j]=0;
					while(father[k]==j){
						if(del[k]==0){
                            //	cout<<"start2 "<<k<<", len="<<len[k]<<","<<father[k]<<endl;
							if(value[k]+len[k]>len[j]){
								len[j]=value[k]+len[k];
							}
						}
						k--;
					}
                    cout<<"over2 "<<k<<","<<father[k]<<" "<<j<<",len:"<<len[j]<<endl;
					k=k1;
					while(father[k]==j){
						if(del[k]==0){
                            //	cout<<"start3 "<<k<<","<<father[k]<<endl;
							if(value[k]+len[k]>len[j]){
								len[j]=value[k]+len[k];
							}
						}
						k++;
					}
                     cout<<"over4 "<<k<<","<<father[k]<<" "<<j<<",len:"<<len[j]<<endl;
				}
				//else if(leaf[i]==1&&father[i]==-1) leaf[i]=-1;
				//shuchu();
			} shuchu();
		}
		void solution1(){
			int i,j,k,j0;
			givelen();
			shuchu();
			while(nopass()==0){
			for(i=0;i<n;i++){
				if(pass[i]==0&&leaf[i]==1&&father[i]!=-1){
					//leaf[i]=-1;
					j=i;
					k=0;
					while(del[j]==0&&father[j]!=-1){
						pass[j]=1;
						k=k+value[j];
						j0=j;
						j=father[j];
						if(del[j]==1) break;
						if(k<d&&len[j]>d) break;
						if(k<d){
							cout<<i<<"-----del "<<j<<" j0="<<j0<<endl;
							//father[j0]=-1;
							del[j]=1;
							pass[j]=1;
							//j=father[j];
							//if(j!=-1) leaf[j]=1;
							r0++;
							break;
						}
					}
				}
				else if(leaf[i]==1&&father[i]==-1) leaf[i]=-1;
			}
			givelen();
			shuchu();
			cout<<endl<<r0<<endl;
			}
		}
		void solution(){
			solution0();
			cout<<r0;
		}

        void digraph(ostream &out = cout) {
            out << "digraph {" << endl;
            for(int i = 0; i < n; i++) {
                out << "\t" << i << (del[i] ? "[color=red];" : ";") << endl;
            }
            for(int i = 1; i < n; i++) {
                out << "\t" << father[i] << "->" << i
                    << "[label=" << value[i]
                    << (del[i] || del[father[i]] ? ",style=dashed];" : "];") << endl;
            }
            out << "}";
        }

};
int main() {
	int n, d;

    ifstream fin("1000_295.txt", ios::in);
    fin >> n >> d;
    dTree dt(n, d, fin);
    fin.close();
    dt.solution();

    ofstream fout("sy3_tj3.gv", ios::out);
    dt.digraph(fout);
    fout.close();

	return 0;
}